<?php
/**
 * Created by PhpStorm.
 * User: openote
 * Date: 2018/12/10
 * Time: 19:41
 */

namespace app\admin\controller;


use app\admin\model\EvaluationsQuestion;
use app\admin\validate\EQ;
use think\Request;

class EqController extends BaseController {
    public function index( $id){
        $eq = EvaluationsQuestion::where('eid', $id)->paginate(10);
        $this->assign('eq', $eq);
        $this->assign('eid', $id);
        return view('index');
    }

//     TODO：还差选择班级
    public function add(Request $request) {
        $num = ['1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J'];
        $eid = $request->param('eid');
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\EQ');
            if (true !== $result)
                return $this->error($result, 'add');

            $content = [];
            for ($i = 1 ; $i <= 10 ; $i++){
                $element = 'content'.$i;
                if($param["$element"] != ""){
                    $first = $num[$i];
                    $content["$first"] = "$param[$element]";
                }
            }
            for ($i = 1 ; $i <= 10 ; $i++){
                $element1 = 'content'.$i;
                unset($param["$element1"]);
            }
            $content = json_encode($content,true);

            $param['addtime'] = time();
            $param['cotent'] = $content;
            $res = EvaluationsQuestion::create($param);
            if ($res) {
                return $this->success('添加成功', url('index', ['id'=>$eid]));
            } else {
                return $this->error('添加失败', 'add');
            }
        } else {
            $this->assign('eid', $eid);
            $this->assign('num', $num);
            return view('add');
        }
    }

    public function edit(Request $request, $eid, $id) {
        $eq = EvaluationsQuestion::get($id);
        $num = ['1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J'];
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\EQ');
            if (true !== $result)
                return $this->error($result, url('edit', ['eid'=>$eid, 'id'=>$id]));

            $content = [];
            for ($i = 1 ; $i <= 10 ; $i++){
                $element = 'content'.$i;
                if($param["$element"] != ""){
                    $first = $num[$i];
                    $content["$first"] = "$param[$element]";
                }
            }
            for ($i = 1 ; $i <= 10 ; $i++){
                $element1 = 'content'.$i;
                unset($param["$element1"]);
            }
            $content = json_encode($content,true);

            $param['addtime'] = time();
            $param['cotent'] = $content;
            $res = $eq->isUpdate(true)->save($param);
            if ($res) {
                return $this->success('编辑成功', url('index', ['id'=>$eid]));
            } else {
                return $this->error('编辑失败', url('edit', ['eid'=>$eid, 'id'=>$id]));
            }
        } else {

            //处理题目数组
            $old_content = json_decode($eq->cotent);
            $old_con = [];
            foreach ($num as $k => $v){
                if (isset($old_content->$v)){
                    $old_con["$k"] = $old_content->$v;
                }else{
                    $old_con["$k"] = "";
                }
            }
            $old_answer = explode(',',$eq->answer);
            $str = '';
            foreach ($old_answer as $old_an){
                $str = $str.$old_an;
            }
            $eq->answer = $str;
//            'num','topic','old_con'
            $this->assign('eid', $eid);
            $this->assign('eq', $eq);
            $this->assign('num', $num);
            $this->assign('old_con', $old_con);
            return view('edit');
        }
    }

    /**
     * 删除
     * @param $eid
     * @param $id
     * @throws \think\exception\DbException
     */
    public function del($eid, $id) {
        $res = EvaluationsQuestion::destroy($id);
        if ($res) {
            return $this->success('删除成功',  url('index', ['id'=>$eid]));
        } else {
            return $this->error('删除失败',  url('index', ['id'=>$eid]));
        }
    }
}